<style src="./index.scss" lang="scss" scoped rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
    export default {
        mixins: [Vue.prototype.$vFN.Mixins.Base],
        computed: {},
        data() {
            return {
                // 修改价格数据
                modifyAmountData: [],
                item: null,
                goodsList: [],
                title: "修改价格",
                // 窗口显示
                boxVisible: false,
                // 运费
                freight: {
                    num: 0,
                    price: 0,
                    total: 0,
                },
                // 实收总金额
                payAmount: 0,
            };
        },
        methods: {
            numBlur(e, index_) {
                if (this.modifyAmountData[index_].num == undefined) {
                    this.modifyAmountData[index_].num = 0;
                }
                this.updatePayAmount()
            },
            numChange(e, index_) {
                if (this.modifyAmountData[index_].num != undefined) {
                    this.updatePayAmount()
                }
                
            },
            freightBlur(e, index_) {
                if (this.freight.num == undefined) {
                    this.freight.num = 0;
                }
                this.updatePayAmount()
            },
            freightChange(e, index_) {
                if (this.freight.num != undefined) {
                    this.updatePayAmount()
                }
                
            },
            async submit() {

                let _modifyAmountItemLst = [];
                this.modifyAmountData.forEach(item => {
                    _modifyAmountItemLst.push({
                        itemId: item.id,
                        productChangeAmount: item.num,
                    })
                })
                let _data = await this.$vFN.Http.post({
                    url: "store/order/modify-amount",
                    data: {
                        orderId: this.item.id,
                        freightChangeAmount: this.freight.num,
                        modifyAmountItemLst: _modifyAmountItemLst,
                    },
                },window);
                if (_data._ok) {
                    this.$vFN.Tools.messageSuccess("操作成功");
                    this.editBoxClose();
                    this.onSuccess();
                }
            },
            // 成功回调
            onSuccess() {},
            // 取消回调
            onCancel() {},
            // 失败回调
            onError() {},
            // 关闭窗口
            editBoxClose() {
                this.boxVisible = false;
            },
            // 取消
            editBoxCancel() {
                this.editBoxClose();
            },

            show(e, item) {
                console.log(e, item);
            },
            updatePayAmount(){
                let _payAmount = 0;
                this.modifyAmountData.forEach(item=>{

                    let _t = this.$vFN.Utils.countNumber_add(item.num, item.price)
                    _payAmount = this.$vFN.Utils.countNumber_add(_payAmount, _t);
                })
                let _f = this.$vFN.Utils.countNumber_add(this.freight.num, this.freight.price)
                _payAmount=this.$vFN.Utils.countNumber_add(_payAmount, _f);
                this.payAmount = _payAmount
            },
        },
        
        async mounted() {
            let _list = []
            this.item.orderItemLst.forEach(item => {
                _list.push({
                    id: item.id,
                    // 加减
                    num: item.changeAmount,
                    // 原实收
                    price: this.$vFN.Utils.countNumber_sub(item.payAmount, item.changeAmount),
                    // 合计
                    total: item.payAmount,

                })
            })
            this.modifyAmountData = _list;
            // 运费
            if (this.item.orderFreight) {
                this.freight = {
                    num: this.item.orderFreight.changeAmount,
                    price: this.$vFN.Utils.countNumber_sub(this.item.orderFreight.payAmount, this.item
                        .orderFreight.changeAmount),
                    total: this.item.orderFreight.payAmount,
                }
            }

            this.updatePayAmount();
            this.boxVisible = true;

        },
        beforeCreate() {},
    };
</script>